package sdkshell.kuaiyan.com.kuaiyansdk.manager;

import android.text.TextUtils;

import com.wmcsk.KuaiyanSdk;
import com.wmcsk.dl.util.LogUtils;
import com.wmcsk.dl.util.SPUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import sdkshell.kuaiyan.com.kuaiyansdk.BuildConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.SdkInit;
import sdkshell.kuaiyan.com.kuaiyansdk.bean.ApkNames;
import sdkshell.kuaiyan.com.kuaiyansdk.bean.LocalPluginBean;
import sdkshell.kuaiyan.com.kuaiyansdk.util.FileUtil;
import sdkshell.kuaiyan.com.kuaiyansdk.util.PluginUtil;

import static sdkshell.kuaiyan.com.kuaiyansdk.util.FileUtil.INSTALL_PATH;


/**
 * Created by wm on 2017/7/10.
 */
public class LocalPluginMager {
    private static LocalPluginMager localPluginMager;
    private Map<String,LocalPluginBean> pluginMagerMap;
    private LocalPluginMager(){
        pluginMagerMap = new HashMap<>();
    }
    public static LocalPluginMager getInstance(){
        if(localPluginMager == null){
            synchronized (NetWorkPluginManager.class){
                if(localPluginMager == null){
                    localPluginMager = new LocalPluginMager();
                }
            }
        }
        return localPluginMager;
    }
    public LocalPluginBean getLocalPluginBean(String apkName){
       return pluginMagerMap.get(apkName);
    }
    public void copyAssertPlugin(String apkName){
        LocalPluginBean localPluginBean = null;
        try {
           localPluginBean = FileUtil.firstCopyAssert(SdkInit.getContext(), apkName);
        }catch (Exception e){
            LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob(e.getMessage());
        }
        if(localPluginBean == null|| TextUtils.isEmpty(localPluginBean.installPath)){
            LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("拷贝本地文件插件出错，开始读取assert目录");
            localPluginBean = FileUtil.copyAssert(SdkInit.getContext(),apkName);
            if(localPluginBean == null||localPluginBean.installPath == null||!new File(localPluginBean.installPath).exists()){
                LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("拷贝assert目录出错");
                LocalPluginBean errPluginBean = new LocalPluginBean();
                errPluginBean.sdkVersion = BuildConfig.VERSION_CODE;
                errPluginBean.pluginVersion = -1;
                pluginMagerMap.put(apkName,errPluginBean);
                checkNetworkPlugin(apkName);
            }else{
                LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("拷贝本地文件插件出错，启用assert目录");
                if(localPluginBean.sdkVersion!=BuildConfig.VERSION_CODE){
                    LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("assert plugin version is not match this sdk version: pluginSdkVersion = "+localPluginBean.sdkVersion+" project sdk version = "+BuildConfig.VERSION_CODE);
                }
                pluginMagerMap.put(apkName,localPluginBean);
                checkNetworkPlugin(apkName);
            }
        }else {
            pluginMagerMap.put(apkName,localPluginBean);
            checkNetworkPlugin(apkName);
        }
    }

    /**
     * 检查网络plugin包
     */
    public void checkNetworkPlugin(String apkName){
        LocalPluginBean localPluginBean = pluginMagerMap.get(apkName);
        File netWrokFile = new File(FileUtil.getDownCacheDir(SdkInit.getContext(),FileUtil.NETWORK_PATH),apkName);
        if(netWrokFile.exists()){
            if(!PluginUtil.checkVersion(netWrokFile.getAbsolutePath(),SdkInit.getContext())){
                LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("主工程版本不一致:"+PluginUtil.getSdkVersion(netWrokFile.getAbsolutePath(),SdkInit.getContext()));
            }else {
                double code = PluginUtil.getPluginVersion(netWrokFile.getAbsolutePath(), SdkInit.getContext());
                double localPluginCode = localPluginBean.pluginVersion;
                if (code > localPluginCode) {
                    try {
                        if(TextUtils.isEmpty(localPluginBean.installPath)){
                            localPluginBean.installPath = new File(FileUtil.getDownCacheDir(SdkInit.getContext(),INSTALL_PATH).getAbsolutePath(), FileUtil.APK_NAME).getAbsolutePath();
                        }
                        FileUtil.doCopyFile(netWrokFile, new File(localPluginBean.installPath), true);
                        netWrokFile.delete();
                        localPluginBean.pluginVersion = code;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pluginPath", localPluginBean.installPath);
            jsonObject.put("pluginVersion",localPluginBean.pluginVersion);
            jsonObject.put("sdkVersion",localPluginBean.sdkVersion);
            SPUtil.saveString(SdkInit.getContext(), ApkNames.get(apkName),jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
